<template>
  <el-container>
    <el-header>四喜商城</el-header>
    <el-main>

      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">

        <el-form-item prop="username">
          账号:<el-input v-model="ruleForm.username" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          密码:<el-input type="password" v-model="ruleForm.password" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <el-button type="primary" @click="addForm()">注册</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
export default {
  name: "User",
    data() {
      var checkAge = (rule, value, callback) => {
        if (value==='') {
          return callback(new Error('账号不能为空'));
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      return {
        ruleForm: {
          username:'',
          password: '',
        },
        rules: {
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          username: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.axios.post("http://localhost:9999/user/loginuser",this.ruleForm).then((res)=>{
          console.log(res);
          if (res.data.code=="200"){
            localStorage.setItem("token",res.data.obj);
            this.$message.success("登陆成功!");
             this.$router.push({path:"/"});
          }else{
            this.$message.error(res.data.obj);
          }
        })

      },
      addForm(){
        this.$router.push({path:"/enro"});
      }

    }
}
</script>

<style scoped>

</style>